exports.main = async context => {
  const cloud = context.cloud
  const pfAppSettingName = 'pfAppSetting'

  try {
    const pfAppSetting = cloud.db.collection(pfAppSettingName);
    const setting = (await pfAppSetting.find())[0]
    setting.authFile = context.data.configUrl

    await clear(pfAppSetting);
    const insertedId = await pfAppSetting.insertOne(setting);

    return {
      success: true,
      result: insertedId
    }
  } catch (error) {
    return {
      success: false,
      error
    };
  }
};

function clear(collection) {
  return collection.deleteMany({
    _id: { $type : 'string' }
  });
}